package xml;

import java.io.File;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class NodeRemover extends NodeUtilities implements INodeRemover {

	@Override
	public void removeNode(String filename, String nodeValue) {
		try {
			if (!new File(filename).exists()) {
				return;
			}
			Document document = super.getDocument(filename);

			if (filename.compareTo(defaultPlaylist) == 0) {
				super.setNodeName(nodeNamePlaylist);
			} else {
				super.setNodeName(nodeNameImage);
			}

			NodeList directory = document.getElementsByTagName(super.getNodeName());

			for (int i = 0; i < directory.getLength(); i++) {
				Node nodo = directory.item(i);
				if (nodo.getFirstChild().getNodeValue().compareTo(nodeValue) == 0) {
					nodo.getParentNode().removeChild(nodo);
				}
			}
			super.saveDocument(filename, document);
		} catch (Exception e) {

		}

	}
}
